package jml.defaultimple.util;

import java.util.Comparator;

public class GenericSortOK<T> implements Comparator<T>
{
	private String sortPattern;
	
	public GenericSortOK(String sortPattern)
	{
		this.sortPattern=sortPattern;
	}
	
	// genre+artist+year
	@Override
	public int compare(T t1, T t2)
	{
		int ret=0;
		boolean seguir=true;
		String atts[]=UString.split(sortPattern,"+");
		
		for(int i=0; i<atts.length && seguir; i++)
		{
			Object o1 = UBean.invokeGetter(t1, atts[i]);
			Object o2 = UBean.invokeGetter(t2, atts[i]);
			
			String v1=(o1!=null)?o1.toString():"";
			String v2=(o2!=null)?o2.toString():"";
			
			ret=v1.compareTo(v2);
			
			seguir=(ret==0);
		}
		
		return ret;
	}
}
